<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">添加</el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">批量删除</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.id" clearable placeholder="请输入ID" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" @selection-change="setSelectRows">
      <el-table-column show-overflow-tooltip type="selection" />
      <el-table-column label="id" prop="id" show-overflow-tooltip />
      <el-table-column label="出租类型" show-overflow-tooltip>
        <template v-slot="scope">
          {{ formatSpecialType(scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column label="出租状态" show-overflow-tooltip>
        <template v-slot="scope">
          {{specialState(scope.row.state) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="居室" prop="houseNum" show-overflow-tooltip />
      <el-table-column label="卫生间" prop="toiletNum" show-overflow-tooltip /> -->
      <el-table-column label="整套面积" prop="houseArea" show-overflow-tooltip />
      <!-- <el-table-column label="卧室类型" prop="roomType" show-overflow-tooltip />
      <el-table-column label="房间号" prop="roomCode" show-overflow-tooltip />
      <el-table-column label="房屋面积" prop="roomArea" show-overflow-tooltip />
      <el-table-column label="朝向" prop="direction" show-overflow-tooltip /> -->
      <el-table-column label="押金" prop="cashPledge" show-overflow-tooltip />
      <el-table-column label="楼层" prop="floor" show-overflow-tooltip />
      <el-table-column label="楼房类型" prop="stepType" show-overflow-tooltip />
      <el-table-column label="起租日期" prop="startDate" show-overflow-tooltip />
      <el-table-column label="到租日期" prop="endDate" show-overflow-tooltip />
      <!-- <el-table-column label="房源描述" prop="introduce" show-overflow-tooltip /> -->
      <el-table-column label="房东姓名" prop="ownerName" show-overflow-tooltip />
      <el-table-column label="房东电话" prop="ownerPhone" show-overflow-tooltip />
      <el-table-column label="房产id" prop="propertyId" show-overflow-tooltip />
      <el-table-column label="房产名称" prop="propertyName" show-overflow-tooltip />
      <!-- <el-table-column label="具体地址" prop="address" show-overflow-tooltip /> -->
      <el-table-column label="总楼层" prop="allFloor" show-overflow-tooltip />
      <el-table-column label="房牌号" prop="houseNo" show-overflow-tooltip />

      <el-table-column label="房东姓名" prop="ownerName" show-overflow-tooltip />
      <el-table-column label="修改时间" prop="updateTime" show-overflow-tooltip />
      <el-table-column label="操作" show-overflow-tooltip width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getList } from '@/api/house/room'
  import Edit from './components/HouseRoomEdit'

  export default {
    name: 'RoomManagement',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          id: '',
        },
        timeOutID: null,
      }
    },
    created() {
      this.fetchData()
    },

    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: [row.id] })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data, totalCount } = await getList(this.queryForm)
        this.list = data.list
        this.total = data.totalCount
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
      formatSpecialType(type) {
        console.log('type',type)
        // 可以根据实际需求进行特殊类型的格式化
        if (type === 0) {
          return '月租'
        } else if (type === 1) {
          return '季租'
        } else if (type === 2) {
          return '半年租'
        } else if (type === 3) {
          return '年租'
        }else{
          return '其他'
        }
      },
      specialState(type) {
        // 可以根据实际需求进行特殊类型的格式化
        if (type === 0) {
          return '待出租'
        } else if (type === 1) {
          return '出租中'
        } else if (type === 2) {
          return '已出租'
        } else if (type === 3) {
          return '已下架'
        }else{
          return '其他'
        }
      },
    },
  }
</script>
